package pro.shushi.pamirs.demo.core.action;


import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import pro.shushi.pamirs.demo.api.enumeration.DemoExpEnumerate;
import pro.shushi.pamirs.demo.api.model.*;
import pro.shushi.pamirs.meta.annotation.Action;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.Model;
import pro.shushi.pamirs.meta.common.exception.PamirsException;
import pro.shushi.pamirs.meta.constant.FunctionConstants;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.enmu.FunctionTypeEnum;
import pro.shushi.pamirs.meta.enmu.ViewTypeEnum;

import java.util.ArrayList;
import java.util.List;


@Component
@Model.model(ProductPlan.MODEL_MODEL)
public class ProductPlanAction {

    @Transactional
    @Action.Advanced(name = FunctionConstants.create, managed = true)
    @Action(displayName = "确定", summary = "创建", bindingType = ViewTypeEnum.FORM)
    @Function(name = FunctionConstants.create)
    @Function.fun(FunctionConstants.create)
    public ProductPlan create(ProductPlan data) {
        checkData(data);
        List<ProductStock> productStocks = data.getProductStocks();
        productStocks.forEach(stock -> {
            stock.construct();
            stock.setLast(stock.getTotal());
        });
        data.construct();
        data.create();

        data.fieldSave(ProductPlan::getProductStocks);
        productStocks.forEach(productStock -> {
            productStock.fieldSave(ProductStock::getProductPrices);
        });

        return data;
    }

    @Function(openLevel = FunctionOpenEnum.API, summary = "选择产品后联动")
    @Function.Advanced(displayName = "选择产品后联动", type = FunctionTypeEnum.QUERY)
    public ProductPlan constructProductChange(ProductPlan data) {
        if(data.getProduct() == null){
            return data;
        }
        Product product = new Product().queryById(data.getProduct().getId());
        product.fieldQuery(Product::getCruise);
        product.getCruise().fieldQuery(Cruise::getRoomTypes);
        List<RoomType> roomTypes = product.getCruise().getRoomTypes();
        List<ProductStock> productStocks = new ArrayList<>();
        for (RoomType roomType : roomTypes) {
            ProductStock productStock = new ProductStock();
            productStock.setRoomType(roomType);
            productStock.setStatus(Boolean.TRUE);
            productStock.setTotal(roomType.getNumber());
            productStock.setProductPrices(buildProductPrice(roomType));
            productStocks.add(productStock);
        }
        data.setProductStocks(productStocks);
        return data;
    }

    private List<ProductPrice> buildProductPrice(RoomType roomType) {
        roomType.fieldQuery(RoomType::getTicketTypes);

        List<ProductPrice> productPrices = new ArrayList<>();
        List<TicketType> ticketTypes = roomType.getTicketTypes();
        if (CollectionUtils.isEmpty(ticketTypes)) {
            return productPrices;
        }

        ticketTypes.forEach(ticketType -> {
            ProductPrice productPrice = new ProductPrice();
            productPrice.setTicketType(ticketType);
            productPrice.setTicketTypeId(ticketType.getId());
            productPrice.setStatus(Boolean.TRUE);
            productPrices.add(productPrice);
        });
        return productPrices;
    }

    // 各种数据校验写这里， 这块可以补充其他校验
    private void checkData(ProductPlan data) {
        List<ProductStock> productStocks = data.getProductStocks();
        productStocks.forEach(productStock -> {
            if (productStock.getTotal()==null) {
                throw PamirsException.construct(DemoExpEnumerate.PRODUCT_STOCK_TOTAL_ISNULL).errThrow();
            }

            List<ProductPrice> productPrices = productStock.getProductPrices();
            if (CollectionUtils.isEmpty(productPrices)) {
                throw PamirsException.construct(DemoExpEnumerate.PRODUCT_STOCK_PRICE_ISNULL).errThrow();
            }
            productPrices.forEach(productPrice -> {
                if (productPrice.getPrice()==null || productPrice.getPrice()==null) {
                    throw PamirsException.construct(DemoExpEnumerate.PRODUCT_PRICE_PRICE_ISNULL).errThrow();
                }
            });
        });
    }

}
